/**
 * 
 */
package com.xb.cloud.merchantapi.product.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xb.cloud.common.client.bo.PageBo;
import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ProductSkuManageApiService;
import com.xb.cloud.order.service.client.ProductStockService;
import com.xb.cloud.product.category.service.client.bo.SkuRequestBO;
import com.xb.cloud.product.merchant.service.client.EndMerchantUserService;
import com.xb.cloud.product.merchant.service.client.ProductSkuDetailService;
import com.xb.cloud.product.merchant.service.client.ProductSkuManageService;
import com.xb.cloud.product.merchant.service.client.ProductSkuService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ProductSkuBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuInputBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuPicInputBo;
import com.xb.cloud.product.service.client.SkuService;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.service.client.bo.ProductSkuStockBo;

/**
 * 商品管理，上下架
 * @author kexue
 *
 */
@Service
public class ProductSkuManageApiServiceImpl implements ProductSkuManageApiService {

	@Resource
	private ProductSkuManageService productSkuManageService;
	
	@Resource
	private ProductStockService productStockService;
	
	@Resource
	private EndMerchantUserService endMerchantUserService;

	@Autowired
	private ProductSkuService productSkuService;
	
	//TODO 买家端service
	@Autowired
	private SkuService skuService;
	
	//TODO 
	@Autowired
	private ProductSkuDetailService productSkuDetailService;
	/**
	 * 获取仓库中sku列表
	 * 
	 * @param isCanSale
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public RtnInfo getSkuListByIsCanSale(Long endMerchantUserId, byte isCanSale, Integer pageNum, Integer pageSize) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		ServiceReturnInfo skuListRetrunInfo = productSkuManageService.getSkuListByIsCanSale(endMerchantUserBo, isCanSale, pageNum, pageSize);
		
		if(ServiceReturnUtils.isSuccess(skuListRetrunInfo)){
			PageBo<ProductSkuBo> pageBo = (PageBo<ProductSkuBo>) skuListRetrunInfo.getData();
			List<ProductSkuBo> skuBoList = pageBo.getRows();
			 
			Map<Long,ProductSkuBo> productIdSkuBoMap=new HashMap<Long,ProductSkuBo>();
			if(skuBoList !=null && skuBoList.size()>0){
				for(ProductSkuBo skuBo:skuBoList){
					productIdSkuBoMap.put(skuBo.getProductId(),skuBo);
				}
			}
			if(productIdSkuBoMap==null || productIdSkuBoMap.keySet().size()==0){
				return ReturnUtils.success(pageBo);
			}else{
				//查询库存信息
				List<Long> productList=new ArrayList<Long>();
				productList.addAll(productIdSkuBoMap.keySet());
				ServiceReturnInfo productStockMapReturnInfo = productStockService
						.getProductStockByProductListShopWarehouse(productList, endMerchantUserBo.getShopId(),
								DbConstants.DEFAULT_WAREHOUSE_ID);
				
				if(ServiceReturnUtils.isSuccess(productStockMapReturnInfo)){
					Map<Long,ProductSkuStockBo> productStockMap=(Map<Long, ProductSkuStockBo>) productStockMapReturnInfo.getData();
					if(productStockMap!=null ){
						for(Long productId:productStockMap.keySet()){
							ProductSkuStockBo skuStockBo = productStockMap.get(productId);
							ProductSkuBo skuBo = productIdSkuBoMap.get(productId);
							//赋值skuBo
							skuBo.setRealStockNum(skuStockBo.getRealStockNum());
							skuBo.setRealFrozenStockNum(skuStockBo.getRealFrozenStockNum());
						}	
					}
					return ReturnUtils.success(pageBo);
				}else{
					//TODO 库存查询失败
					return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
				}
			}
			
		}else{
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	/**
	 * 上架、
	 * 
	 * @return
	 */
	@Override
	public RtnInfo skuUp(Long endMerchantUserId, Long skuId) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		

		ServiceReturnInfo skuListRetrunInfo = productSkuManageService.skuUp(endMerchantUserBo, skuId);
		if(ServiceReturnUtils.isSuccess(skuListRetrunInfo)){
			return ReturnUtils.success(skuListRetrunInfo.getData());
		}else{
			//TODO 错误类型转换
			return ReturnUtils.error(skuListRetrunInfo.getCode(),skuListRetrunInfo.getMessage(),skuListRetrunInfo.getDetailMessage());
		}
		
	}

	/**
	 * 下架、
	 * 
	 * @return
	 */
	public RtnInfo skuDown(Long endMerchantUserId, Long skuId) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		

		ServiceReturnInfo skuListRetrunInfo = productSkuManageService.skuDown(endMerchantUserBo, skuId);
		if(ServiceReturnUtils.isSuccess(skuListRetrunInfo)){
			return ReturnUtils.success(skuListRetrunInfo.getData());
		}else{
			//TODO 错误类型转换
			return ReturnUtils.error(skuListRetrunInfo.getCode(),skuListRetrunInfo.getMessage(),skuListRetrunInfo.getDetailMessage());
		}
	}
	
	/** 
	 * 修改价格
	 */
	public RtnInfo updatePrice(Long endMerchantUserId, Long skuId, BigDecimal price) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		ServiceReturnInfo skuListRetrunInfo = productSkuManageService.updatePrice(endMerchantUserBo, skuId,price);
		if(ServiceReturnUtils.isSuccess(skuListRetrunInfo)){
			return ReturnUtils.success(skuListRetrunInfo.getData());
		}else{
			//TODO 错误类型转换
			return ReturnUtils.error(skuListRetrunInfo.getCode(),skuListRetrunInfo.getMessage(),skuListRetrunInfo.getDetailMessage());
		}
	}
	

	@Override
	public RtnInfo updateStock(Long endMerchantUserId, Long skuId, Long realStockNum) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		//获取skuId对应的ProductSkuBo(库存表里定位一条库存要shopid和productid)
		ProductSkuBo productSkuBo=null;
		ServiceReturnInfo serviceReturnInfo=productSkuService.getSkuById(skuId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			productSkuBo=(ProductSkuBo) serviceReturnInfo.getData();
		} else {
			return ReturnUtils.error(ReturnCodes.SKU_NOT_EXIST);
		}
		
		ServiceReturnInfo serviceRtnInfo=productStockService.updateStock(endMerchantUserBo, productSkuBo, realStockNum);
		if(ServiceReturnUtils.isSuccess(serviceRtnInfo)){
			return ReturnUtils.success(serviceRtnInfo.getData());
		}else{
			//TODO 错误类型转换
			return ReturnUtils.error(serviceRtnInfo.getCode(),serviceRtnInfo.getMessage(),serviceRtnInfo.getDetailMessage());
		}
	}
	
	/**
	 * 查看商品的库存信息详情
	 */
	 public RtnInfo getStockBoBySkuId(Long endMerchantUserId,Long skuId){
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		
		//获取skuId对应的ProductSkuBo(库存表里定位一条库存要shopid和productid)
		ProductSkuBo productSkuBo=null;
		ServiceReturnInfo serviceReturnInfo=productSkuService.getSkuById(skuId);
		if(ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			productSkuBo=(ProductSkuBo) serviceReturnInfo.getData();
		} else {
			return ReturnUtils.error(ReturnCodes.SKU_NOT_EXIST);
		}

		ServiceReturnInfo serviceRtnInfo = productStockService.getProductStockByProductShopWarehouse(
			productSkuBo.getProductId(), productSkuBo.getShopId(), DbConstants.DEFAULT_WAREHOUSE_ID);
		if (ServiceReturnUtils.isSuccess(serviceRtnInfo)) {
			return ReturnUtils.success(serviceRtnInfo.getData());
		} else {
			return ReturnUtils.error(serviceRtnInfo.getCode(), serviceRtnInfo.getMessage(),
					serviceRtnInfo.getDetailMessage());
		}
	 }

	/**
	 * 查看商品的详情(参考buy)
	 */
	@Override
	public RtnInfo getSKUDetail(Long skuId) {
		 
		ServiceReturnInfo serviceRtnInfo = productSkuDetailService.getSKUDetail(skuId);
		if (ServiceReturnUtils.isSuccess(serviceRtnInfo)) {
			return ReturnUtils.success(serviceRtnInfo.getData());
		} else {
			return ReturnUtils.error(serviceRtnInfo.getCode(), serviceRtnInfo.getMessage(),
					serviceRtnInfo.getDetailMessage());
		}
	}
	
	/**
	 * 查看商品的价格(参考buy)
	 */
	@Override
	public RtnInfo getSKUPrice(Long skuId) {
		SkuRequestBO requestBO=new SkuRequestBO();
		requestBO.setSkuID(skuId);
		ServiceReturnInfo serviceRtnInfo = skuService.getSKUPrice(requestBO);
		if (ServiceReturnUtils.isSuccess(serviceRtnInfo)) {
			return ReturnUtils.success(serviceRtnInfo.getData());
		} else {
			return ReturnUtils.error(serviceRtnInfo.getCode(), serviceRtnInfo.getMessage(),
					serviceRtnInfo.getDetailMessage());
		}
	}

	@Override
	public RtnInfo skuDelete(Long currentMerchantUserId, Long skuId) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(currentMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
		

		ServiceReturnInfo skuListRetrunInfo = productSkuManageService.skuDelete(endMerchantUserBo, skuId);
		if(ServiceReturnUtils.isSuccess(skuListRetrunInfo)){
			return ReturnUtils.success(skuListRetrunInfo.getData());
		}else{
			//TODO 错误类型转换
			return ReturnUtils.error(skuListRetrunInfo.getCode(),skuListRetrunInfo.getMessage(),skuListRetrunInfo.getDetailMessage());
		}
	}

	/**
	 * 更新sku分两个库，
	 * 
	 * @param endMerchantUserBo
	 * @param productId
	 * @param skuPics
	 * @param detailContentJson
	 * @param skuInput
	 * @param priceSale
	 * @param stock:库存
	 * @return
	 */
	@Override
	public RtnInfo updateProductSku(Long endMerchantUserId, Long productId,
			List<ProductSkuPicInputBo> skuPics, String detailContentJson, ProductSkuInputBo skuInput, 
			BigDecimal priceSale,BigDecimal priceMarket,Long stockNum) {
		EndMerchantUserBo endMerchantUserBo = null;
		// 获取当前merchantId/shopId对应的商家用户
		ServiceReturnInfo merchantReturnInfo = endMerchantUserService.getEndMerchantUserById(endMerchantUserId);
		if (ServiceReturnUtils.isSuccess(merchantReturnInfo)) {
			if (merchantReturnInfo.getData() == null) {
				return ReturnUtils.error(ReturnCodes.MERCHANT_NOT_EXIST);
			} else {
				endMerchantUserBo = (EndMerchantUserBo) merchantReturnInfo.getData();
			}
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		// 更新product库相关表.
		ServiceReturnInfo productSku = productSkuManageService.skuUpdate(endMerchantUserBo,
				productId, skuPics, detailContentJson, skuInput, priceSale, priceMarket);

		Long productSkuId = null;
		if (ServiceReturnUtils.isSuccess(productSku)) {
			productSkuId = (Long) productSku.getData();
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}

		/**
		 * TODO 异常回滚操作pending. 插入user库库存表
		 */
		ProductSkuBo productSkuBo=new ProductSkuBo();
		productSkuBo.setShopId(endMerchantUserBo.getShopId());
		productSkuBo.setProductId(productId);
		ServiceReturnInfo serviceRtnInfo = productStockService.updateStock(endMerchantUserBo,
				productSkuBo, stockNum);
		if (ServiceReturnUtils.isSuccess(serviceRtnInfo)) {
			return ReturnUtils.success(productSkuId);
		} else {
			return ReturnUtils.error(ReturnCodes.SYSTEM_EXCEPTION);
		}
	}

	 
}
